﻿namespace OP10.ViewModels
{
    public class ST04LeftMotorCheckViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;

        public ST04LeftMotorCheckViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

           
            InitInputParam();
            //开启定时器任务
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(200);
            dispatcherTimer.Tick += OnTick;
            dispatcherTimer.Start();

        }

        private void InitInputParam()
        {
            Step1ForceIn=LeftMotorParam_Global.Step1_ForceIN;
            Step2ForceIn = LeftMotorParam_Global.Step2_ForceIN;
            CountSet=LeftMotorParam_Global.SetCount_R;
        
        }
        #region 私有方法


        private void OnTick(object? sender, EventArgs e)
        {
            Step1Pos=LeftMotorPLCToPC_Global.Step1_Pos/10000;
            Step1Press=LeftMotorPLCToPC_Global.Step1_Press;
            Step2Pos=LeftMotorPLCToPC_Global.Step2_Pos/10000;
            Step2Press=LeftMotorPLCToPC_Global.Step2_Press;
            SmacLfactor=LeftMotorPLCToPC_Global.SMAC_Lfactor;
            Lconstant=LeftMotorPLCToPC_Global.Lconstant;
            CurrentCount=LeftMotorPLCToPC_Global.ActualCount_L;

            CheckDone = LeftMotorPLCToPC_Global.CheckDone;
            CheckStep1Done = LeftMotorPLCToPC_Global.Step1_ForceIN_Req;
            CheckStep2Done = LeftMotorPLCToPC_Global.Step2_ForceIN_Req;
            atHome=LeftMotorPLCToPC_Global.atHome;
        }
        #endregion

        #region 事件
        private DelegateCommand _GoBackCommand;

        public DelegateCommand GoBackCommand =>
            _GoBackCommand ?? (_GoBackCommand = new DelegateCommand(ExecuteGoBackCommand));

        void ExecuteGoBackCommand()
        {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST04DeviceManagerView);
        }


        private DelegateCommand _DownDownCmd;
        public DelegateCommand DownDownCmd =>
            _DownDownCmd ?? (_DownDownCmd = new DelegateCommand(ExecuteDownDownCmd));

        void ExecuteDownDownCmd()
        {
            LeftMotorPCToPLC_Global.SoftLoad=true;
            Step1ForceIn = 0;
            Step2ForceIn = 0;
        }


        private DelegateCommand _DownUpCmd;
        public DelegateCommand DownUpCmd =>
            _DownUpCmd ?? (_DownUpCmd = new DelegateCommand(ExecuteDownUpCmd));

        void ExecuteDownUpCmd()
        {
            LeftMotorPCToPLC_Global.SoftLoad = false;

        }


        private DelegateCommand _Step1DownCmd;
        public DelegateCommand Step1DownCmd =>
            _Step1DownCmd ?? (_Step1DownCmd = new DelegateCommand(ExecuteStep1DownCmd));

        void ExecuteStep1DownCmd()
        {
            LeftMotorPCToPLC_Global.Step1_StartBT = true;
        }



        private DelegateCommand _Step1UpCmd;
        public DelegateCommand Step1UpCmd =>
            _Step1UpCmd ?? (_Step1UpCmd = new DelegateCommand(ExecuteStep1UpCmd));

        void ExecuteStep1UpCmd()
        {
            LeftMotorPCToPLC_Global.Step1_StartBT = false;
        }


        private DelegateCommand _Step2DownCmd;
        public DelegateCommand Step2DownCmd =>
            _Step2DownCmd ?? (_Step2DownCmd = new DelegateCommand(ExecuteStep2DownCmd));

        void ExecuteStep2DownCmd()
        {
            LeftMotorPCToPLC_Global.Step2_StartBT = true;
        }


        private DelegateCommand _Step2UpCmd;
        public DelegateCommand Step2UpCmd =>
            _Step2UpCmd ?? (_Step2UpCmd = new DelegateCommand(ExecuteStep2UpCmd));

        void ExecuteStep2UpCmd()
        {
            LeftMotorPCToPLC_Global.Step2_StartBT = false;
        }


        private DelegateCommand _ResetDownCmd;
        public DelegateCommand ResetDownCmd =>
            _ResetDownCmd ?? (_ResetDownCmd = new DelegateCommand(ExecuteResetDownCmd));

        void ExecuteResetDownCmd()
        {
            LeftMotorPCToPLC_Global.Reset=true;
        }


        private DelegateCommand _ResetUpCmd;
        public DelegateCommand ResetUpCmd =>
            _ResetUpCmd ?? (_ResetUpCmd = new DelegateCommand(ExecuteResetUpCmd));

        void ExecuteResetUpCmd()
        {
            LeftMotorPCToPLC_Global.Reset = false;

        }

        private DelegateCommand _BackOriginDownCmd;
        public DelegateCommand BackOriginDownCmd =>
            _BackOriginDownCmd ?? (_BackOriginDownCmd = new DelegateCommand(ExecuteBackOriginDownCmd));

        void ExecuteBackOriginDownCmd()
        {
            LeftMotorPCToPLC_Global.GoHome = true;
            

        }


        private DelegateCommand _BackOriginUpCmd;
        public DelegateCommand BackOriginUpCmd =>
            _BackOriginUpCmd ?? (_BackOriginUpCmd = new DelegateCommand(ExecuteBackOriginUpCmd));

        void ExecuteBackOriginUpCmd()
        {
           LeftMotorPCToPLC_Global.GoHome=false;
        }




        private DelegateCommand<string> _TextChangeCommand;
        public DelegateCommand<string> TextChangeCommand =>
            _TextChangeCommand ?? (_TextChangeCommand = new DelegateCommand<string>(ExecuteTextChangeCommand));

        void ExecuteTextChangeCommand(string p)
        {
            switch (p)
            {

                case "Step1ForceIn":
                  LeftMotorParam_Global.Step1_ForceIN=Step1ForceIn;
                    break;

                case "Step2ForceIn":
                    LeftMotorParam_Global.Step2_ForceIN=Step2ForceIn;
                    break;

                case "CountSet":
                    LeftMotorParam_Global.SetCount_R = CountSet;
                    break;


                default:
                    break;
            }
        }

        private DelegateCommand _NavigateRightCmd;
        public DelegateCommand NavigateRightCmd =>
            _NavigateRightCmd ?? (_NavigateRightCmd = new DelegateCommand(ExecuteNavigateRightCmd));

        void ExecuteNavigateRightCmd()
        {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST04RightMotorCheckView);
        }
        #endregion



        #region 属性


        private float _Step1ForceIn;
        public float Step1ForceIn
        {
            get { return _Step1ForceIn; }
            set { SetProperty(ref _Step1ForceIn, value); }
        }


        private float _Step2ForceIn;
        public float Step2ForceIn
        {
            get { return _Step2ForceIn; }
            set { SetProperty(ref _Step2ForceIn, value); }
        }


        private int _Step1Pos;
        public int Step1Pos
        {
            get { return _Step1Pos; }
            set { SetProperty(ref _Step1Pos, value); }
        }


        private int _Step1Press;
        public int Step1Press
        {
            get { return _Step1Press; }
            set { SetProperty(ref _Step1Press, value); }
        }

        private int _Step2Pos;
        public int Step2Pos
        {
            get { return _Step2Pos; }
            set { SetProperty(ref _Step2Pos, value); }
        }

        private int _Step2Press;
        public int Step2Press
        {
            get { return _Step2Press; }
            set { SetProperty(ref _Step2Press, value); }
        }

        private int _CountSet;
        public int CountSet
        {
            get { return _CountSet; }
            set { SetProperty(ref _CountSet, value); }
        }

        private int _CurrentCount;
        public int CurrentCount
        {
            get { return _CurrentCount; }
            set { SetProperty(ref _CurrentCount, value); }
        }

        private bool _CheckDone;
        public bool CheckDone
        {
            get { return _CheckDone; }
            set { SetProperty(ref _CheckDone, value); }
        }

        private float _SmacLfactor;
        public float SmacLfactor
        {
            get { return _SmacLfactor; }
            set { SetProperty(ref _SmacLfactor, value); }
        }


        private float _Lconstant;
        public float Lconstant
        {
            get { return _Lconstant; }
            set { SetProperty(ref _Lconstant, value); }
        }


        private bool _CheckStep1Done;
        public bool CheckStep1Done
        {
            get { return _CheckStep1Done; }
            set { SetProperty(ref _CheckStep1Done, value); }
        }

        private bool _CheckStep2Done;
        public bool CheckStep2Done
        {
            get { return _CheckStep2Done; }
            set { SetProperty(ref _CheckStep2Done, value); }
        }

        private bool _atHome;
        public bool atHome
        {
            get { return _atHome; }
            set { SetProperty(ref _atHome, value); }
        }
        #endregion
    }
}
